<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 基础设置
 * Class Base
 * @author heyu
 */
class Base extends MY_Controller {
    /**
     * @var AdmSettingModel
     */
    public $mSetting = null;

	public function __construct(){
		parent::__construct();
        $this->loadModel('AdmSettingModel', 'mSetting');
	}
	
	/**
	 * 首页
	 */
	public function index(){
        $info = $this->mSetting->get('sharp_title,sharp_url,sharp_icon,sharp_desc,aftersale_reason,
        app_merchant_version,app_merchant_update_description,app_merchant_android_download_url,
        app_merchant_android_code,app_consumer_version,app_consumer_update_description,app_consumer_android_download_url,
        app_consumer_android_code,app_consumer_ios_download_url,app_merchant_ios_download_url,regiser_integral_number,
        sign_integral_number,publish_integral_number,share_integral_number,integral_coefficient,register_experience_value_number,
        sign_experience_value_number,publish_experience_value_number,share_experience_value_number,experience_value_coefficient,
        rank_experience,merchant_comment_tags,consumer_ads,aftersale_limite_day,web_service_qq,web_service_tel,web_merchant_service_tel,
        merchant_distance_limit,change_order_percent,hot_search,share_percent,app_ios_update,app_android_update');
        $info['aftersale_reason'] = empty($info['aftersale_reason']) ?  array() : json_decode($info['aftersale_reason'], true);
        $info['hot_search'] = empty($info['hot_search']) ?  array() : json_decode($info['hot_search'], true);
        $info['consumer_ads'] = $info['consumer_ads']?json_decode($info['consumer_ads'], true):array();
        //ios更新信息
        $app_ios_update = $info['app_ios_update']?json_decode($info['app_ios_update'], true):array();
        $info['consumer_ios_version'] = isset($app_ios_update[0]['version'])?$app_ios_update[0]['version']:'';
        $info['consumer_ios_update_description'] = isset($app_ios_update[0]['description'])?$app_ios_update[0]['description']:'';
        $info['consumer_ios_is_forced'] = isset($app_ios_update[0]['is_forced'])?$app_ios_update[0]['is_forced']:0;
        $info['merchant_ios_version'] = isset($app_ios_update[1]['version'])?$app_ios_update[1]['version']:'';
        $info['merchant_ios_update_description'] = isset($app_ios_update[1]['description'])?$app_ios_update[1]['description']:'';
        $info['merchant_ios_is_forced'] = isset($app_ios_update[1]['is_forced'])?$app_ios_update[1]['is_forced']:0;
        //安卓更新信息
        $app_android_update = $info['app_android_update']?json_decode($info['app_android_update'], true):array();
        $info['consumer_android_is_forced'] = isset($app_android_update[0]['is_forced'])?$app_android_update[0]['is_forced']:0;
        $info['merchant_android_is_forced'] = isset($app_android_update[1]['is_forced'])?$app_android_update[1]['is_forced']:0;

        if (strlen($info['rank_experience']) > 0) {
            $info['rank_experience'] = json_decode($info['rank_experience'], true);
        }
        $info['merchant_comment_tags'] = empty($info['merchant_comment_tags']) ?  array() : json_decode($info['merchant_comment_tags'], true);
        $data = array(
            'info' => $info,
            'title' => '基础设置'
        );
		$this->view('config/base', $data, 'validation,cropit_master,webuploader');
	}

    /**
     * 保存数据
     */
    public function save(){
        $vConf = array(
            array('sharp_title', '分享标题', 'required'),
            array('sharp_url', '分享地址', 'required'),
            array('sharp_desc', '分享描述', 'required'),
            array('app_merchant_version', '商家端安卓版本号', 'required'),
            array('app_merchant_update_description', '商家端安卓更新内容描述', 'required'),
            array('app_merchant_android_download_url', '商家端安卓下载地址', 'required'),
            array('app_merchant_android_code', '商家端安卓版本编号', 'required'),
            array('app_consumer_version', '用户端安卓版本号', 'required'),
            array('app_consumer_update_description', '用户端安卓更新内容描述', 'required'),
            array('app_consumer_android_download_url', '用户端安卓下载地址', 'required'),
            array('app_consumer_android_code', '用户端安卓版本编号', 'required'),
            array('consumer_ios_version', '用户端ios版本号', 'required'),
            array('consumer_ios_update_description', '用户端ios更新内容描述', 'required'),
            array('consumer_ios_is_forced', '用户端ios是否强制更新', 'required'),

            array('app_consumer_ios_download_url', '用户端苹果下载地址', 'required'),
            array('app_merchant_ios_download_url', '商家端苹果下载地址', 'required'),
            array('merchant_ios_version', '商家端ios版本号', 'required'),
            array('merchant_ios_update_description', '商家端ios更新内容描述', 'required'),
            array('merchant_ios_is_forced', '商家端ios是否强制更新', 'required'),
            array('regiser_integral_number', '注册赠送积分数量', 'required|is_numeric'),
            array('sign_integral_number', '签到赠送积分数量(每日限一次)', 'required|is_numeric'),
            array('publish_integral_number', '发布信息赠送数量(每日首次)', 'required|is_numeric'),
            array('share_integral_number', '分享赠送数量( 每日限5次)', 'required|is_numeric'),
            array('integral_coefficient', '在线购买积分获得系数', 'required'),
            array('register_experience_value_number', '注册经验奖励数量', 'required|is_numeric'),
            array('sign_experience_value_number', '签到经验奖励数量(每日限一次)', 'required|is_numeric'),
            array('publish_experience_value_number', '发布信息经验奖励数量(每日首次)', 'required|is_numeric'),
            array('share_experience_value_number', '分享经验奖励数量( 每日限5次)', 'required|is_numeric'),
            array('experience_value_coefficient', '在线购买获得经验奖励系数', 'required'),
            array('aftersale_limite_day', '申请售后时间', 'required|is_numeric'),
            array('merchant_distance_limit', '商家配送范围', 'required|integer'),
            array('change_order_percent', '订单商品退差价百分比', 'required'),
            array('share_percent', '分享赠送百分比', 'required'),
        );
        $this->formDataValidation($vConf);
        $aftersale_reason = post('aftersale_reason');
        $aftersale_reason = isset($aftersale_reason) ? json_encode($aftersale_reason, JSON_UNESCAPED_UNICODE) : '';
        $hot_search = post('hot_search');
        $hot_search = isset($hot_search) ? json_encode($hot_search, JSON_UNESCAPED_UNICODE) : '';
        $comment = post('comment');
        $comment = isset($comment) ? json_encode($comment, JSON_UNESCAPED_UNICODE) : '';
        $rankExperience = post('rank_experience');
        $images = post('consumer_ads'); //用户引导广告图
        if (!empty($rankExperience)){
            $score = 0;
            foreach ($rankExperience as $k => $v){
                if (!isset($v['min_experience']) || !isset($v['max_experience'])){
                    unset($rankExperience[$k]);
                }
                if (strlen($v['rank_name']) == 0){
                    $this->ajaxReturn(0, '请填写区间名称');
                }
                if ($v['max_experience'] != -1 && $v['min_experience'] >= $v['max_experience']){
                    $this->ajaxReturn(0, '最大范围必须大于最小范围');
                }
                if ($score > 0 && $score != $v['min_experience']){
                    $this->ajaxReturn(0, '衔接之前值必须相同');
                }
                $score = $v['max_experience'];
            }
            if ($score > 0){
                $this->ajaxReturn(0, '请添加最小到无限的范围');
            }
            $rankExperience = json_encode($rankExperience);
        }
        $appios = array(
            array('version' => trim(post('consumer_ios_version')),'download_url' => trim(post('app_consumer_ios_download_url')),'description' => trim(post('consumer_ios_update_description')),'is_forced' => trim(post('consumer_ios_is_forced'))),
            array('version' => trim(post('merchant_ios_version')),'download_url' => trim(post('app_merchant_ios_download_url')),'description' => trim(post('merchant_ios_update_description')),'is_forced' => trim(post('merchant_ios_is_forced')))
        );
        $appAndroid = array(
            array('version' => trim(post('app_consumer_version')),'code' => trim(post('app_consumer_android_code')),'download_url' => trim(post('app_consumer_android_download_url')),'description' => trim(post('app_consumer_update_description')),'is_forced' => trim(post('consumer_android_is_forced'))),
            array('version' => trim(post('app_merchant_version')),'code' => trim(post('app_merchant_android_code')),'download_url' => trim(post('app_merchant_android_download_url')),'description' => trim(post('merchant_ios_update_description')),'is_forced' => trim(post('merchant_android_is_forced')))
        );
        $data = array(
            'sharp_title' => trim(post('sharp_title')),
            'sharp_url' => trim(post('sharp_url')),
            'sharp_desc' => trim(post('sharp_desc')),
            'web_service_qq' => trim(post('web_service_qq')),
            'web_service_tel' => trim(post('web_service_tel')),
            'web_merchant_service_tel' => trim(post('web_merchant_service_tel')),
            'aftersale_reason' => $aftersale_reason,
            'aftersale_limite_day' => (int)post('aftersale_limite_day'),
            'merchant_comment_tags' => $comment,
            'app_merchant_version' => post('app_merchant_version'),
            'app_merchant_update_description' => trim(post('app_merchant_update_description')),
            'app_merchant_android_download_url' => trim(post('app_merchant_android_download_url')),
            'app_merchant_android_code' => trim(post('app_merchant_android_code')),
            'app_consumer_version' => post('app_consumer_version'),
            'app_consumer_update_description' => trim(post('app_consumer_update_description')),
            'app_consumer_android_download_url' => trim(post('app_consumer_android_download_url')),
            'app_consumer_android_code' => trim(post('app_consumer_android_code')),
            'app_consumer_ios_download_url' => trim(post('app_consumer_ios_download_url')),
            'app_merchant_ios_download_url' => trim(post('app_merchant_ios_download_url')),
            'regiser_integral_number' => (int)post('regiser_integral_number'),
            'sign_integral_number' => (int)post('sign_integral_number'),
            'publish_integral_number' => (int)post('publish_integral_number'),
            'share_integral_number' => (int)post('share_integral_number'),
            'integral_coefficient' => post('integral_coefficient'),
            'register_experience_value_number' => (int)post('register_experience_value_number'),
            'sign_experience_value_number' => (int)post('sign_experience_value_number'),
            'publish_experience_value_number' => (int)post('publish_experience_value_number'),
            'share_experience_value_number' => (int)post('share_experience_value_number'),
            'experience_value_coefficient' => post('experience_value_coefficient'),
            'merchant_distance_limit' => post('merchant_distance_limit'),
            'change_order_percent' => post('change_order_percent'),
            'share_percent' => post('share_percent'),
            'rank_experience' => $rankExperience,
            'hot_search' => $hot_search,
            'app_ios_update' => json_encode($appios, JSON_UNESCAPED_UNICODE),
            'app_android_update' => json_encode($appAndroid, JSON_UNESCAPED_UNICODE),

        );
        $icon = post('icon');
        if($icon){
            $path = 'uploads/admin/config/share/icon/';
            $tA = saveCSVimg(array($icon), $path, 'icon');
            if( ! $tA){
                $this->ajaxReturn(0, '图标上传失败');
            }
            $data['sharp_icon'] = $tA[0];
        }
        /**
         *  移动新文件
         */
        $imgPath = array();
        $path = 'uploads/admin/config/ads';
        if ($images){
            foreach ($images as $k=>$v){
                if (stripos($v,'temp') !== FALSE){
                    $size = getimagesize($v);
                    $imgPath[] = saveTempImg($v,$path,true, $size[0], $size[1]);
                }else {
                    $imgPath[] = str_replace(base_url(), '', $v);
                }
            }
            $data['consumer_ads'] = json_encode($imgPath);
        }

        if($this->mSetting->save($data)){
            $this->ajaxReturn(1, '保存成功', null, getUrl('config/base'));
        }
        $this->ajaxReturn(0, '保存失败');
    }
}
